package com.maistars.spider.infrastructure.adapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dhr
 * @date 2021/1/17 上午12:15
 */
public class JsoupAdapter {

    public static Map<String, String> getHtmlContent(String url, int timeout) {
        Map<String, String> result = new HashMap<>();
        result.put("status", "0");
        result.put("url", url);
        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(timeout).get();
        } catch (Exception e) {
            result.put("status", "1");
            result.put("result", "can't load this page");
            return result;
        }
        result.put("result", document.html());
        return result;
    }

    public static Document parse(String htmlContent) {
        return Jsoup.parse(htmlContent);
    }

    public static Document parse(File in, String charsetName) throws IOException {
        return Jsoup.parse(in, charsetName);
    }

    public static Document parseBodyFragment(String bodyHtml, String baseUri) {
        return Parser.parseBodyFragment(bodyHtml, baseUri);
    }

    public static String removeElementsBySelector(String htmlContent, String[] selectors) {
        Document document = parse(htmlContent);
        for (String selector : selectors) {
            Elements removedEles = document.select(selector);
            if (removedEles != null) {
                removedEles.remove();
            }
        }
        return document.html();
    }
}
